#ifndef _FILELOADERIMP_H_
#define _FILELOADERIMP_H_

/** This file assumes that the following macros are defined:

		SEPARATOR :  a character specifying the separator used in the file
		COLUMN_TYPES : a list specifying the types for each of the columns in the file
			the special type NONE specifies that the column is not used
			Each element in the list is a tuple (column_name, type)

		MAX_LINE_SIZE : is the maximum size of a line. If not defined, it is set to 10000
		TUPLES_PER_CHUNK : the number of tuples in each chunk
		PAGE_SIZE_4K_MULT : the size of the page as a 4K multiplier

		These definitions are expected in the file FileLoaderDefs.m4

		The same definitions are used in FileLoader.cc.m4
*/















#include "EventProcessor.h"
#include "Message.h"
#include "DataTypes.h"
#include "DistributedCounter.h"


/** class to populate columns with values and send them off to a file writer */
class FileLoaderImp : public EventProcessorImp {
private:
	off_t chunksProduced; // how many chunks we produced so far
	off_t tuplesHandled; // how many tuples we have handled so far
	DistributedCounter* dCounter; // distributed counter to detect when all the loaders are done
	off_t pageSize; // the size of the page (to know how to allocate the memory with mmap)

  objectHolder* el_tuples; //memory map for all the tuple objects
  INT* l_indiciesData;
  IDENTIFIER* l_sortedkeyData;						    	  
  IDENTIFIER* l_orderkeyData; // column l_orderkey array   			    	  

  IDENTIFIER* l_sortedpartkeyData; // column l_partkey array                                    
  IDENTIFIER* l_sortedsupkeyData; // column l_supkey array                              
  INT* l_sortedlinenumberData; // column l_linenumber array                                     
  FLOAT* l_sortedquantityData; // column l_quantity array                               
  FLOAT* l_sortedextendedpriceData; // column l_extendedprice array                                     
  FLOAT* l_sorteddiscountData; // column l_discount array                               
  FLOAT* l_sortedtaxData; // column l_tax array                                 
  CHAR<1>* l_sortedreturnflagData; // column l_returnflag array                                 
  CHAR<1>* l_sortedlinestatusData; // column l_linestatus array                                 
  DATE* l_sortedshipdateData; // column l_shipdate array                                
  DATE* l_sortedcommitdateData; // column l_commitdate array                                    
  DATE* l_sortedreceiptdateData; // column l_receiptdate array                                  
  CHAR<25>* l_sortedshipinstructData; // column l_shipinstruct array                                    
  CHAR<10>* l_sortedshipmodeData; // column l_shipmode array                                
  long int* l_sortedcommentData; // offset pointer for column l_comment
  char* l_sortedcommentContent; // content of column l_comment


  IDENTIFIER* l_partkeyData; // column l_partkey array   			    	  
  IDENTIFIER* l_supkeyData; // column l_supkey array   			    	  
  INT* l_linenumberData; // column l_linenumber array   			    	  
  FLOAT* l_quantityData; // column l_quantity array   			    	  
  FLOAT* l_extendedpriceData; // column l_extendedprice array   			    	  
  FLOAT* l_discountData; // column l_discount array   			    	  
  FLOAT* l_taxData; // column l_tax array   			    	  
  CHAR<1>* l_returnflagData; // column l_returnflag array   			    	  
  CHAR<1>* l_linestatusData; // column l_linestatus array   			    	  
  DATE* l_shipdateData; // column l_shipdate array   			    	  
  DATE* l_commitdateData; // column l_commitdate array   			    	  
  DATE* l_receiptdateData; // column l_receiptdate array   			    	  
  CHAR<25>* l_shipinstructData; // column l_shipinstruct array   			    	  
  CHAR<10>* l_shipmodeData; // column l_shipmode array   			      
  long int* l_commentData; // offset pointer for column l_comment
  char* l_commentContent; // content of column l_comment
     	
	
	EventProcessor fileWriter; // the file writer that can write the chunks
	char* fileName; // the name of the file that we need to scan
	FILE* stream; // the file descriptor
	bool finished; // turns to true when the whole file is read

	// automate the filling of chunks
	void FillAndSendChunk(void);
	// stop function
	void Done(void);

public:
	// the arguments to the constructor are the file containing the data
	// and the file writer that can write the data
	FileLoaderImp(const char* _fileName, EventProcessor& _fileWriter, DistributedCounter* _dCounter);

	// destructor
	virtual ~FileLoaderImp(void);

	// Message handler for the return OK
	MESSAGE_HANDLER_DECLARATION(ChunkWrittenProc);


};

#endif // _FILELOADERIMP_H_
